home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 16.2 KB | 552 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: ClockPar.cpp
- // Release Version: $ 1.0d1 $
- //
- // Author: Lonnie Millett
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef CLOCKPAR_H
- #include "ClockPar.h"
- #endif
-
- #ifndef CLOCKFRA_H
- #include "ClockFra.h"
- #endif
-
- #ifndef CLOCKSEL_H
- #include "ClockSel.h"
- #endif
-
- // ----- Framework Includes -----
-
- #ifndef BCCOLL_H
- #include <BCColl.h>
- #endif
-
- #ifndef FWMEMMGR_H
- #include <FWMemMgr.h>
- #endif
-
- #ifndef FWUTIL_H
- #include "FWUtil.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _STORAGEU_
- #include <StorageU.h>
- #endif
-
- #ifndef _FRAME_
- #include <Frame.h>
- #endif
-
- #ifndef _SHAPE_
- #include <Shape.h>
- #endif
-
- #ifndef _WINDOW_
- #include <Window.h>
- #endif
-
- #ifndef _MENUBAR_
- #include <MenuBar.h>
- #endif
-
- #ifndef _FOCI_
- #include <Foci.h>
- #endif
-
- #ifndef _FOCUSSET_
- #include <FocusSet.h>
- #endif
-
- #ifndef _XMPSESSN_
- #include <XMPSessM.h>
- #endif
-
- #ifndef _ARBITRAT_
- #include <Arbitrat.h>
- #endif
-
- #ifndef _DISPTCH_
- #include <Disptch.h>
- #endif
-
- #ifndef _EXCEPT_
- #include <Except.h>
- #endif
-
- #ifndef _STDPROPS_
- #include <StdProps.h>
- #endif
-
- #ifndef _CMDDEFS_
- #include <CmdDefs.h>
- #endif
-
- #ifndef _TRANSLAT_
- #include <Translat.h>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__STDTYPES__)
- #include <StdTypes.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__PACKAGES__)
- #include <Packages.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__RESOURCES__)
- #include <Resources.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
- #include <Sound.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__TOOLUTILS__)
- #include <ToolUtils.h>
- #endif
-
- //==============================================================================
- // Constants
- //==============================================================================
-
- const short kClockTypeItemID = 1;
-
- const short kClockSoundsNoneItemID = 1;
- const short kClockSoundsTickItemID = 2;
- const short kClockSoundsChimeItemID = 3;
- const short kClockSoundsBothItemID = 4;
- const short kClockPlayChimeSoundItemID = 6;
-
- const short kClockSoundsNone = 1;
- const short kClockSoundsTick = 2;
- const short kClockSoundsChime = 3;
- const short kClockSoundsBoth = 4;
-
- const XMPFocusType kClockAudioFocus = "Audio";
-
- const short kClockIdleFreq = 15;
-
- #pragma segment cclockpart
-
- //----------------------------------------------------------------------------------------
- // CClockPart::CClockPart
- //----------------------------------------------------------------------------------------
- CClockPart::CClockPart() :
- fLastTime(0),
- fClockType(kAnalogClock),
- fClockSounds(kClockSoundsChime),
- fChimeSound(NULL),
- fTickSound(NULL),
- fBackgroundPICT(NULL),
- fAudioFocusToken(kXMPNullFocus),
- fAudioFocusSet(NULL),
- fAudioFocusFrame(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::~CClockPart
- //----------------------------------------------------------------------------------------
- CClockPart::~CClockPart()
- {
- if (fChimeSound)
- FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)fChimeSound);
- fChimeSound = NULL;
-
- if (fTickSound)
- FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)fTickSound);
- fTickSound = NULL;
-
- if (fBackgroundPICT)
- ::KillPicture(fBackgroundPICT);
- fBackgroundPICT = NULL;
-
- delete fAudioFocusSet;
- fAudioFocusSet = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::Release
- //----------------------------------------------------------------------------------------
- void CClockPart::Release()
- {
- FW_CPart::Release();
-
- if (this->GetRefCount() == 1)
- {
- this->GetSession()->GetDispatcher()->UnregisterIdle(this, NULL);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::Initialize
- //----------------------------------------------------------------------------------------
- void CClockPart::Initialize()
- {
- FW_CPart::Initialize();
-
- XMPSession* session = this->GetSession();
-
- // Register a special focus for audio so the arbitrator can arbitrate the audio focus
- fAudioFocusToken = session->Tokenize(kClockAudioFocus);
-
- if (!session->GetArbitrator()->IsFocusRegistered(fAudioFocusToken))
- session->GetArbitrator()->RegisterFocus(fAudioFocusToken,NULL);
-
- FW_CAcquireASLMResourceAccess aq;
-
- XMPMenuBar *menuBar = this->GetMenuBar();
- menuBar->AddMenuLast(kClockTypeMenu, GetMenu(kClockTypeMenu), this);
- menuBar->AddMenuLast(kClockSoundsMenu, GetMenu(kClockSoundsMenu), this);
-
- menuBar->RegisterCommand(cClockType, kClockTypeMenu, kClockTypeItemID);
- menuBar->RegisterCommand(cClockSoundsNone, kClockSoundsMenu, kClockSoundsNoneItemID);
- menuBar->RegisterCommand(cClockSoundsTick, kClockSoundsMenu, kClockSoundsTickItemID);
- menuBar->RegisterCommand(cClockSoundsChime, kClockSoundsMenu, kClockSoundsChimeItemID);
- menuBar->RegisterCommand(cClockSoundsBoth, kClockSoundsMenu, kClockSoundsBothItemID);
- menuBar->RegisterCommand(cClockPlayChimeSound, kClockSoundsMenu, kClockPlayChimeSoundItemID);
- menuBar->CheckCommand(cClockSoundsChime, TRUE);
-
- // Get default clock sounds
- fChimeSound = ::GetResource(soundListRsrc, kClockChime);
- ::DetachResource(fChimeSound);
-
- fTickSound = ::GetResource(soundListRsrc, kClockTick);
- ::DetachResource(fTickSound);
-
- // Create a new focus set which just contains the speaker. This focus will be
- // aquired before playing the clock sounds
- fAudioFocusSet = new XMPFocusSet;
- fAudioFocusSet->InitFocusSet();
- fAudioFocusSet->Add(this->GetAudioFocusToken());
-
- unsigned long time;
- GetDateTime(&time);
- this->SetLastTime(time);
-
- this->GetSession()->GetDispatcher()->RegisterIdle(this, NULL, kClockIdleFreq);
- }
-
- //------------------------------------------------------------------------------
- // CClockPart::InternalizeContent
- //------------------------------------------------------------------------------
- void CClockPart::InternalizeContent(XMPStorageUnit* storageUnit)
- {
- storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
-
- storageUnit->GetValue(sizeof(fClockType), (XMPValue)&fClockType);
- storageUnit->GetValue(sizeof(fClockSounds), (XMPValue)&fClockSounds);
-
- unsigned long chimeSize = storageUnit->GetSize();
- FW_PlatformHandle chimeHandle = FW_CMemoryManager::AllocateSystemHandle(chimeSize);
- THROW_IF_NULL(chimeHandle);
-
- FW_CMemoryManager::LockSystemHandle(chimeHandle);
- storageUnit->GetValue(chimeSize, (XMPValue)*chimeHandle);
- FW_CMemoryManager::UnlockSystemHandle(chimeHandle);
- this->SetChimeSound(chimeHandle);
-
- FW_Boolean hasPicture;
- storageUnit->GetValue(sizeof(FW_Boolean), (XMPValue)&hasPicture);
- if (hasPicture)
- {
- unsigned long pictSize = storageUnit->GetSize();
- PicHandle pictHandle = (PicHandle) FW_CMemoryManager::AllocateSystemHandle(pictSize);
- THROW_IF_NULL(pictHandle);
-
- FW_CMemoryManager::LockSystemHandle((FW_PlatformHandle)pictHandle);
- storageUnit->GetValue(pictSize, (XMPValue)*pictHandle);
- FW_CMemoryManager::UnlockSystemHandle((FW_PlatformHandle)pictHandle);
- this->SetBackgroundPICT(pictHandle);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::ExternalizeContent
- //----------------------------------------------------------------------------------------
- void CClockPart::ExternalizeContent(XMPStorageUnit* storageUnit)
- {
- storageUnit->Focus(kXMPPropContents, kXMPPosUndefined, this->GetContentPropertyValueType(), 0, kXMPPosUndefined);
-
- storageUnit->SetValue(sizeof(fClockType), (XMPValue)&fClockType);
- storageUnit->SetValue(sizeof(fClockSounds), (XMPValue)&fClockSounds);
-
- FW_CMemoryManager::LockSystemHandle(fChimeSound);
- storageUnit->SetValue(FW_CMemoryManager::GetSystemHandleSize(fChimeSound), (XMPValue)*fChimeSound);
- FW_CMemoryManager::UnlockSystemHandle(fChimeSound);
-
- if (fBackgroundPICT)
- {
- FW_Boolean hasPicture = TRUE;
- storageUnit->SetValue(sizeof(FW_Boolean), (XMPValue)&hasPicture);
-
- FW_CMemoryManager::LockSystemHandle((FW_PlatformHandle)fBackgroundPICT);
- storageUnit->SetValue(FW_CMemoryManager::GetSystemHandleSize((FW_PlatformHandle)fBackgroundPICT), (XMPValue)*fBackgroundPICT);
- FW_CMemoryManager::UnlockSystemHandle((FW_PlatformHandle)fBackgroundPICT);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::GetContentPropertyValueType
- //----------------------------------------------------------------------------------------
- XMPValueType CClockPart::GetContentPropertyValueType() const
- {
- return kSampleClockKind;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::NewFrame
- //----------------------------------------------------------------------------------------
- FW_CFrame* CClockPart::NewFrame(XMPFrame* xmpFrame, XMPTypeToken presentation)
- {
- FW_UNUSED(presentation);
-
- CClockFrame *frame = new CClockFrame();
- frame->InitClockFrame(xmpFrame, this);
- return frame;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::DoIdle
- //----------------------------------------------------------------------------------------
- FW_Boolean CClockPart::DoIdle()
- {
- unsigned long tickCount;
- DateTimeRec time;
- DateTimeRec lastTime;
-
- ::GetDateTime(&tickCount);
- ::Secs2Date(tickCount, &time);
- ::Secs2Date(this->GetLastTime(), &lastTime);
-
- if (lastTime.second != time.second)
- {
- short clockSound = this->GetClockSounds();
- if (clockSound != kClockSoundsNone)
- {
- // Ask for the audio focus so we can play sound and keep it for as long as we can
- // It would be nice if focus abitration could be at the part level just like idling
- // but for now it belongs to the frame
- FW_Boolean hasSoundFocus = FALSE;
- XMPArbitrator* arbitrator = this->GetSession()->GetArbitrator();
- if (arbitrator->GetFocusOwner(this->GetAudioFocusToken()) != fAudioFocusFrame)
- hasSoundFocus = arbitrator->RequestFocusSet(fAudioFocusSet, fAudioFocusFrame);
- else
- hasSoundFocus = TRUE;
-
- if (hasSoundFocus)
- {
- if ((clockSound == kClockSoundsTick) || (clockSound == kClockSoundsBoth))
- this->PlayTickSound();
- if (((clockSound == kClockSoundsChime) || (clockSound == kClockSoundsBoth)) &&
- (time.minute == 0) && (time.second == 0))
- this->PlayChimeSound();
- }
- }
-
- // Update all the display frames of the same type
- BC_TCollectionActiveIterator<FW_CFrame*> iter(*this->GetDisplayFrameList());
- while (!iter.IsDone())
- {
- ((CClockFrame*)*iter.CurrentItem())->UpdateClock(tickCount);
- iter.Next();
- }
-
- this->SetLastTime(tickCount);
- }
- return TRUE;
- }
-
- //------------------------------------------------------------------------------
- // CClockPart::DoMenuEvent
- //------------------------------------------------------------------------------
- FW_Boolean CClockPart::DoMenuEvent(XMPMenuBar* menuBar, XMPCommandID commandID)
- {
- FW_Boolean menuHandled = FALSE;
-
- switch (commandID)
- {
- case cClockType:
- {
- if (this->GetClockType() == kAnalogClock)
- this->SetClockType(kDigitalClock);
- else
- this->SetClockType(kAnalogClock);
-
- FW_CPartFrameIterator iter(this, this->GetDefaultPresentation());
- while (!iter.IsDone())
- {
- this->FrameShapeChanged(iter.CurrentItem()->GetXMPFrame());
- iter.Next();
- }
- }
-
- menuHandled = TRUE;
- break;
-
- case cClockSoundsNone:
- this->SetClockSounds(kClockSoundsNone);
- menuHandled = TRUE;
- break;
-
- case cClockSoundsTick:
- this->SetClockSounds(kClockSoundsTick);
- menuHandled = TRUE;
- break;
-
- case cClockSoundsChime:
- this->SetClockSounds(kClockSoundsChime);
- menuHandled = TRUE;
- break;
-
- case cClockSoundsBoth:
- this->SetClockSounds(kClockSoundsBoth);
- menuHandled = TRUE;
- break;
-
- case cClockPlayChimeSound:
- this->PlayChimeSound();
- menuHandled = TRUE;
- break;
-
- default:
- menuHandled = FW_CPart::DoMenuEvent(menuBar, commandID);
- }
-
- return menuHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::DoAdjustMenus
- //----------------------------------------------------------------------------------------
- void CClockPart::DoAdjustMenus(XMPMenuBar* menuBar)
- {
- Str255 menuString;
-
- FW_CAcquireASLMResourceAccess aq;
-
- XMPTranslation *translate = GetSession()->GetTranslation();
- XMPType PICTType = translate->GetISOTypeFromPlatformType('PICT', kXMPPlatformDataType);
- XMPType sndType = translate->GetISOTypeFromPlatformType('snd ', kXMPPlatformDataType);
-
- // We should use kXMPApplesnd here but it is currently defined wrong
- // Just type cast the string for right now
- if (HasPropertyOnClipboard(kXMPPropContents, PICTType) ||
- HasPropertyOnClipboard(kXMPPropContents, (XMPValueType)sndType))
- menuBar->EnableCommand(kXMPCommandPaste, TRUE);
-
- if (this->GetClockType() == kAnalogClock)
- GetIndString(menuString, kClockPartStrings, kClockDigitalMenuString);
- else
- GetIndString(menuString, kClockPartStrings, kClockAnalogMenuString);
- menuBar->SetItemString(cClockType, menuString);
-
- // Uncheck them all then check the correct one
- menuBar->CheckCommand(cClockSoundsNone, FALSE);
- menuBar->CheckCommand(cClockSoundsTick, FALSE);
- menuBar->CheckCommand(cClockSoundsChime, FALSE);
- menuBar->CheckCommand(cClockSoundsBoth, FALSE);
-
- switch(this->GetClockSounds())
- {
- case kClockSoundsNone:
- menuBar->CheckCommand(cClockSoundsNone, TRUE);
- break;
- case kClockSoundsTick:
- menuBar->CheckCommand(cClockSoundsTick, TRUE);
- break;
- case kClockSoundsChime:
- menuBar->CheckCommand(cClockSoundsChime, TRUE);
- break;
- case kClockSoundsBoth:
- menuBar->CheckCommand(cClockSoundsBoth, TRUE);
- break;
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // CClockPart::AddDisplayFrame
- //----------------------------------------------------------------------------------------
- void CClockPart::AddDisplayFrame(XMPFrame* xmpFrame)
- {
- FW_CPart::AddDisplayFrame(xmpFrame);
-
- if (!fAudioFocusFrame)
- fAudioFocusFrame = xmpFrame;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::RemoveDisplayFrame
- //----------------------------------------------------------------------------------------
- void CClockPart::RemoveDisplayFrame(XMPFrame* xmpFrame)
- {
- if (xmpFrame == fAudioFocusFrame)
- {
- XMPArbitrator* arbitrator = this->GetSession()->GetArbitrator();
- arbitrator->RelinquishFocusSet(fAudioFocusSet, fAudioFocusFrame);
- fAudioFocusFrame = NULL;
- }
- FW_CPart::RemoveDisplayFrame(xmpFrame);
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::PlayTickSound
- //----------------------------------------------------------------------------------------
- void CClockPart::PlayTickSound()
- {
- ::SndPlay(NULL, fTickSound, TRUE);
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::PlayChimeSound
- //----------------------------------------------------------------------------------------
- void CClockPart::PlayChimeSound()
- {
- ::SndPlay(NULL, fChimeSound, TRUE);
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::NewSelection
- //----------------------------------------------------------------------------------------
- FW_CSelection* CClockPart::NewSelection()
- {
- CClockSelection* clockSelection = new CClockSelection;
- clockSelection->InitClockSelection(this);
- return clockSelection;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::SetChimeSound
- //----------------------------------------------------------------------------------------
- void CClockPart::SetChimeSound(FW_PlatformHandle chimeSound)
- {
- if (fChimeSound)
- FW_CMemoryManager::FreeSystemHandle(fChimeSound);
-
- fChimeSound = chimeSound;
- }
-
- //----------------------------------------------------------------------------------------
- // CClockPart::SetBackgroundPICT
- //----------------------------------------------------------------------------------------
- void CClockPart::SetBackgroundPICT(PicHandle backgroundPICT)
- {
- if (fBackgroundPICT)
- ::KillPicture(fBackgroundPICT);
-
- fBackgroundPICT = backgroundPICT;
- }
-
-